package me.gavin.util

import android.util.Log
import me.gavin.BuildConfig

private const val TAG = "_gavin_"
private const val NULL = "_NULL_"

fun Any?.log(level: Int = Log.DEBUG, tag: String = TAG, prefix: String? = null, suffix: String? = null) {
    if (BuildConfig.DEBUG || level >= Log.ERROR) {
        val msg = "${prefix ?: ""} ${this ?: NULL} ${suffix ?: ""}"
        when (level) {
            Log.VERBOSE -> Log.v(tag, msg)
            Log.INFO -> Log.i(tag, msg)
            Log.WARN -> Log.w(tag, msg)
            Log.ERROR -> Log.e(tag, msg)
            else -> Log.d(tag, msg)
        }
    }
}

fun Throwable?.log(level: Int = Log.WARN, tag: String = TAG, prefix: String? = null, suffix: String? = null) {
    if (BuildConfig.DEBUG || level >= Log.ERROR) {
        val msg = "${prefix ?: ""} ${this?.message ?: NULL} ${suffix ?: ""}"
        when (level) {
            Log.ERROR -> Log.e(tag, msg, this)
            else -> Log.w(tag, msg, this)
        }
    }
}

fun Any?.print() = println(this)